var net = require("net");

var client = new net.Socket();

var clientPort = 8431;

client.setEncoding('utf8');

//客户端关闭写在connect方法里
client.connect(clientPort, 'localhost', function (socket) {
    console.log("连接成功");
    client.write("hello");
    setTimeout(function () {
        client.write("chendmao");
        client.write("over");
    }, 1000);

});

var imsg = "";
//客户端把所有的信息拼接后
client.on("data", function (data) {
    imsg += data;
    //socket.write("确认数据"+data);
    console.log('server接收到客户端数据', imsg);
});

client.on("error", function (error) {
    console.log("和服务器通信过程中发生的错误，错误信息为%s", error.code);
    client.destroy(); //销毁掉端口
});

//断开的匿名函数
setTimeout(function () {
    client.end(' 断开连接');
}, 5000);
